package com.cbgolf.oa.views;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.base.BaseView;
import com.cbgolf.oa.presenter.IPresenterImp;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import com.cbgolf.oa.viewbean.OrderBean;
import com.cbgolf.oa.widget.ListViewNew;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.abslistview.CommonAdapter;

import org.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by lq on 2017/11/2.
 */

public class BillingView extends BaseView {
    //
    @ViewInject(R.id.topBack_ll)
    private View topBack;
    @ViewInject(R.id.title_tv)
    private TextView topTitleTv;
    @ViewInject(R.id.topRight_tv)
    private TextView topRightTv;
    //
    @ViewInject(R.id.a_billing_success_point_name_tv)
    private TextView pointNameTv;
    @ViewInject(R.id.a_billing_success_shopping_num_tv)
    private TextView shopNumTv;
    @ViewInject(R.id.a_billing_success_time_tv)
    private TextView timeTv;
    @ViewInject(R.id.a_billing_success_billing_name_tv)
    private TextView billNameTv;
    @ViewInject(R.id.a_billing_success_listview)
    private ListViewNew listView;
    @ViewInject(R.id.listview_bottom_line)
    private View listViewBottomLine;
    @ViewInject(R.id.a_billing_success_allmoney_tv)
    private TextView allMoneyTv;
    @ViewInject(R.id.a_billing_success_service_money_tv)
    private TextView serviceMoneyTv;
    @ViewInject(R.id.a_billing_success_discount_money_tv)
    private TextView disCountTv;
    @ViewInject(R.id.a_billing_success_favorable_money_tv)
    private TextView youHunTv;
    @ViewInject(R.id.a_billing_success_realmoney_tv)
    private TextView realMoneyTv;
    //
    @ViewInject(R.id.a_billing_success_cardnum_tv)
    private TextView cardNumTv;
    @ViewInject(R.id.a_billing_success_cardname_tv)
    private TextView cardNameTv;
    @ViewInject(R.id.a_billing_success_cardid_tv)
    private TextView cardIdTv;
    @ViewInject(R.id.a_billing_success_phone_tv)
    private TextView phoneTv;
    //
    @ViewInject(R.id.a_billing_success_toplogo_ll)
    private View topLogoView;


    private IPresenterImp worker;

    public BillingView(Activity act, IPresenterImp iWorker) {
        this.worker = iWorker;
        ViewUtils.autoInjectAllField(this, act);
        super.init(act);
    }


    @Override
    protected void setViews() {
        topBack.setVisibility(View.GONE);
        topTitleTv.setText(TextUtil.Text(worker.getActivityTitle()));
        topRightTv.setText("关闭");
        topRightTv.setVisibility(View.VISIBLE);
    }

    @Override
    protected void setListener() {
        topBack.setOnClickListener(v -> worker.back());
        topRightTv.setOnClickListener(v -> worker.back());
    }

    @Override
    public void showProgress() {
        AnimaUtil.showLoading(context);
    }

    @Override
    public void stopProgress() {
        AnimaUtil.stopLoading();
    }

    @Override
    public void getDataSuccess(Object obj) {
        OrderBean bean = (OrderBean) obj;
        if (null == bean) {
            getDataFail(null);
            return;
        }
        showInfo(bean);
    }


    @Override
    public void getDataFail(String msg) {
        if (!Util.isNull(msg)) {
            Util.show(msg);
        } else {
            Util.showNetError();
        }
    }

    @Override
    public void getDataFail(String msg, int code) {
        getDataFail(msg);
    }

    public void showInfo(OrderBean bean) {

        pointNameTv.setText(TextUtil.Text(bean.orderPlace));
        shopNumTv.setText("消费单号：" + TextUtil.Text(bean.consumerOrderNo));
        timeTv.setText("开单时间：" + DateUtil.stampToDate(bean.orderTime));
        billNameTv.setText("开单人员：" + TextUtil.Text(null != bean.orderPerson ? bean.orderPerson.name : ""));
        serviceMoneyTv.setText("服务费" + TextUtil.textNumFloat(bean.serviceCharge) + "元");
        disCountTv.setText("折扣费" + TextUtil.textNumFloat(bean.discountAmount) + "元");
        youHunTv.setText("优惠" + TextUtil.textNumFloat(bean.preference) + "元");
        realMoneyTv.setText("实付：￥" + TextUtil.textNumFloat(bean.payable) + "元");
        cardNumTv.setText("消费卡号：" + TextUtil.Text(bean.consumerCarNo));
        cardIdTv.setText("身份：" + TextUtil.Text(bean.identityName));
        cardNameTv.setText("姓名：" + TextUtil.Text(bean.name));
        phoneTv.setText("联系电话：" + TextUtil.Text(bean.customerPhone));
        allMoneyTv.setText("￥" + TextUtil.Text(bean.totalCost));

        List<OrderBean.goods> list = bean.goodsDetailList;
        if (Util.listIsNull(list)) {
            listView.setVisibility(View.GONE);
            return;
        }
        listView.setVisibility(View.VISIBLE);
        listViewBottomLine.setVisibility(View.VISIBLE);
        CommonAdapter<OrderBean.goods> adapter = new CommonAdapter<OrderBean.goods>(context,
                R.layout.item_order_details, list) {
            @Override
            public void convert(ViewHolder holder, OrderBean.goods goods) {
                holder.setText(R.id.item_order_details_name_tv, TextUtil.Text(goods.goodsName + "    X" + goods.consumeNumber));
                holder.setText(R.id.item_order_details_price_tv, "￥" + TextUtil.textNumFloat(goods.price));
            }
        };
        listView.setAdapter(adapter);
    }

    public void setOrderDetails() {
        topBack.setVisibility(View.VISIBLE);
        topRightTv.setVisibility(View.GONE);
        topLogoView.setVisibility(View.GONE);

    }
}
